package com.example.demo.facade;

/**
 * @className: FacadePatternExample
 * @author: 芃
 * @description:
 * @date: 2024/12/31 11:36
 */
// CPU子系统类
class CPU {
    public void startup() {
        System.out.println("CPU is starting up...");
    }

    public void shutdown() {
        System.out.println("CPU is shutting down...");
    }
}

// 内存子系统类
class Memory {
    public void startup() {
        System.out.println("Memory is starting up...");
    }

    public void shutdown() {
        System.out.println("Memory is shutting down...");
    }
}

// 硬盘子系统类
class Disk {
    public void startup() {
        System.out.println("Disk is starting up...");
    }

    public void shutdown() {
        System.out.println("Disk is shutting down...");
    }
}

// 门面类，提供统一的对外接口来操作电脑启动和关闭相关的复杂子系统
class ComputerFacade {
    private CPU cpu;
    private Memory memory;
    private Disk disk;

    public ComputerFacade() {
        this.cpu = new CPU();
        this.memory = new Memory();
        this.disk = new Disk();
    }

    // 启动电脑的统一方法，内部调用各个子系统的启动方法
    public void startup() {
        System.out.println("Starting computer...");
        cpu.startup();
        memory.startup();
        disk.startup();
        System.out.println("Computer started successfully!");
    }

    // 关闭电脑的统一方法，内部调用各个子系统的关闭方法
    public void shutdown() {
        System.out.println("Shutting down computer...");
        disk.shutdown();
        memory.shutdown();
        cpu.shutdown();
        System.out.println("Computer shut down successfully!");
    }
}

// 测试类
public class FacadePatternExample {
    public static void main(String[] args) {
        ComputerFacade computerFacade = new ComputerFacade();
        // 通过门面类启动电脑，无需关心具体子系统的启动细节
        computerFacade.startup();
        System.out.println("Computer is running...");
        // 通过门面类关闭电脑，同样无需关心具体子系统的关闭细节
        computerFacade.shutdown();
    }
}
